package com.anonymous.controller;

import com.anonymous.entity.Post;
import com.anonymous.entity.Reply;
import com.anonymous.service.PostService;
import com.anonymous.service.ReplyService;
import com.anonymous.util.IpUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.*;

/**
 * (Post)表控制层
 *
 * @author makejava
 * @since 2022-01-16 11:17:38
 */
@Controller
@RequestMapping("post")
public class PostController {
    /**
     * 服务对象
     */
    @Resource
    private PostService postService;
    @Resource
    private ReplyService replyService;


    /**
     * 查询Post及Post下的Reply列表
     * @param postId
     * @return
     */
    @ResponseBody
    @RequestMapping("/postShow.do")
    public Map<String,Object> showPost(@RequestParam(name = "postId") int postId) {
        Post post = new Post();
        post = postService.queryById(postId);
        //根据主贴查询对应回帖,并放入List列表
        List<Reply> replyList;
        replyList=replyService.queryByPostId(postId);
        Map map = new HashMap<>();
        map.put("post",post);
        map.put("replyList",replyList);
        return map;
    }


    /**
     * 实现删除主贴功能, 与之对应的回帖也会相应删除(返回值未确定)
     *
     * @param postId
     * @return
     */
    @ResponseBody
    @RequestMapping("/postDelete.do")
    public String deletePost(@RequestParam(name = "postId") int postId) {
        postService.deleteById(postId);
        replyService.deleteByPostId(postId);
        System.out.println("deleted");
        return "";
    }

    /**
     * 实现发帖功能
     * 还有picture1-9没有写进参数
     * 用户ip应从session获取
     *
     * @param request
     * @param title
     * @param text
     * @return
     */
    @ResponseBody
    @RequestMapping("/post.do")
    public Object doPost(HttpServletRequest request,
                         @RequestParam(name = "title") String title,
                         @RequestParam(name = "text") String text) {
        Post post = new Post();
        post.setUserId(1);
        post.setTitle(title);
        post.setText(text);
        post.setIp(IpUtil.getIpAddr(request));
        post.setIsDeleted(0);
        post.setLikenumber(0);
        post.setRepliesNumber(0);
        post.setPostTime(new Date());
        postService.insert(post);
        return post;
    }

    /**
     * 分页查询
     *
     * @param post        筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Post>> queryByPage(Post post, PageRequest pageRequest) {
        return ResponseEntity.ok(this.postService.queryByPage(post, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Post> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.postService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param post 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Post> add(Post post) {
        return ResponseEntity.ok(this.postService.insert(post));
    }

    /**
     * 编辑数据
     *
     * @param post 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Post> edit(Post post) {
        return ResponseEntity.ok(this.postService.update(post));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.postService.deleteById(id));
    }

}

